<template>
  <div class="gene-auth-pop-container">
    <el-dialog title="授权项目"
               :visible.sync="dialogVisible"
               width="60%"
               :close-on-click-modal="false"
               :before-close="handleClose">
      <el-row :gutter="20">
        <el-form :model="requestParam"
                 ref="requestParam">
          <el-col :span="24"
                  class="gene-pop-content">
            <el-table :data="requestParam.tableData"
                      height="450"
                      border
                      @current-change="handleSelectChange"
                      :header-cell-style="{ background: '#f5f5f5' }"
                      style="width: 100%">
              <el-table-column label=""
                               width="55"
                               align="center"
                               fixed="left">
                <template slot-scope="scope">
                  <el-radio v-model="tableradio"
                            :label="scope.row"><i /></el-radio>
                </template>
              </el-table-column>
              <el-table-column prop="project_code"
                               align="center"
                               show-overflow-tooltip
                               label="项目代码">
              </el-table-column>
              <el-table-column prop="project_name"
                               label="授权项目名称"
                               align="center"
                               show-overflow-tooltip>
              </el-table-column>
              <el-table-column label="是否为关联项目"
                               align="center"
                               :show-overflow-tooltip="true">
                <template slot-scope="scope">
                  <span>{{scope.row.custom_project_type==2?'是':'否'}}</span>
                </template>
              </el-table-column>
              <el-table-column prop="company_name"
                               align="center"
                               show-overflow-tooltip
                               label="所属单位">
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="
                   24"
                  class="bottom-box">
            <el-button @click="onBack">返回</el-button>
            <el-button type="primary"
                       @click="onSure">确定</el-button>
          </el-col>
        </el-form>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { ProjectListApi } from "@/services/ProjectCenter";
export default {
  props: {
    currentPageName: {
      type: String,
      default: () => {
        return ''
      }
    },
    objRowGeneral: {
      type: Object,
      default: () => {
        return {}
      }
    },
    geneAuthItemPopUpDialog: {
      type: Boolean,
      default: () => {
        return false
      }
    }
  },
  data () {
    return {
      objectId: '',
      treeData: [],
      tableradio: {},
      dialogVisible: false,
      requestParam: {//参数
        tableData: [],
        selectId: '',
      }
    }
  },
  methods: {
    getCurrentPageObjId (treeData) {
      treeData.map((item) => {
        if (item.is_dir == 1) {
          this.getCurrentPageObjId(item.children);
          item.children.forEach((item1) => {
            if (item1.object_name == this.currentPageName) {
              this.objectId = '';
              this.objectId = item1.object_id;
            }
          })
        }
      })
    },
    getPageObjListByUser () {//项目权限-获取当前用户可查看的项目页面权限列表
      return ProjectListApi.getPageObjListByUser({
        params: {

        }
      }).then((res) => {
        if (res.data) {
          this.treeData = res.data;
          this.getCurrentPageObjId(this.treeData);//获取当前功能页面的主键值
          console.log('99999 treeData=', this.treeData)
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    handleSelectChange (currentRow) {
      this.tableradio = currentRow
      this.requestParam.selectId = currentRow.project_id
      console.log('当前行的对象', currentRow)
    },
    onSure () {//确定
      if (this.requestParam.selectId == '') {
        this.$message.warning('请任意选中一个授权项目!');
      } else {
        this.$set(this.tableradio, 'object_id', this.objectId);
        this.$emit('update:objRowGeneral', this.tableradio);
        this.onBack();
      }
    },
    onBack () {//返回
      this.tableradio = {};
      this.requestParam = this.$options.data().requestParam;//重置
      this.$emit('update:geneAuthItemPopUpDialog', false);
    },


    handleClose (done) {//弹框关闭
      this.onBack();
      done();
    },
  },
  watch: {
    async geneAuthItemPopUpDialog (n, o) {
      if (n == true) {
        let proList = this.$store.state.choosedProjectList.list || [];//飞轮选中的项目 
        this.requestParam.tableData = proList;
        this.getPageObjListByUser();
        this.dialogVisible = true;
      }
    }
  },
  async created () {
    if (this.geneAuthItemPopUpDialog == true) {
      this.getPageObjListByUser();
      this.requestParam.tableData = this.$store.state.choosedProjectList.list || [];
      console.log('c 2项目名称choosedProjectList=', this.requestParam.tableData)
      this.dialogVisible = true;
    }
  }
}
</script>

<style lang="scss" scoped>
.gene-auth-pop-container {
  .gene-pop-content {
    display: flex;
    flex-direction: row;
  }
  .bottom-box {
    margin-top: 24px;
    text-align: center;
  }
}
</style>